package cn.itrip.auth.service;

import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.common.MD5;
import cn.itrip.common.RedisAPI;
import cn.itrip.dao.user.ItripUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private ItripUserMapper itripUserMapper;
    @Resource
    private MailService mailService;
    @Resource
    private RedisAPI redisAPI;
    @Resource
    private SmsService smsService;
    /**
     * 创建邮箱用户
     */
    public void itriptxCreateUser(ItripUser user) throws Exception {
        //1，添加用户信息
        itripUserMapper.insertItripUser(user);
        //2，生成激活码
        String activationCode= MD5.getMd5(new Date().toLocaleString(),32);
        //3，发送邮件
        mailService.sendActivationMail(user.getUserCode(),activationCode);
        //4，激活码存到Redis中:三十分钟有效
        redisAPI.set("activation:"+user.getUserCode(),30*60,activationCode);

    }

    /**
     * 激活激活码
     * @param mail
     * @param code
     * @return
     * @throws Exception
     */
    @Override
    public boolean activate(String mail, String code) throws Exception {
        //1,验证激活码
        String value=redisAPI.get("activation:"+mail);
        if (value.equals(code)){
            ItripUser user=itripUserMapper.findUserByUserCode(mail);
            if (user!=null){
                //更新用户
                user.setActivated(1);
                user.setFlatID(user.getId());
                user.setUserType(0);
                itripUserMapper.updateItripUser(user);
                return true;
            }
        }
        return false;
    }

    /**
     * 创建手机注册的用户
     * @param user
     * @throws Exception
     */
    public void itriptxCreateUserByPhone(ItripUser user) throws  Exception{
        //创建用户，生成验证码(1111-9999)，发送验证码，缓存验证码到Redis
        itripUserMapper.insertItripUser(user);
        //2，生成验证码
        int code=MD5.getRandomCode();
        //3，发送验证码:使用模板1，时间为5分钟
        smsService.send(user.getUserCode(),"1",new String[]{String.valueOf(code),"5"});
        //4,缓存验证码到Redis:三分钟
        redisAPI.set("activation:"+user.getUserCode(),3*60,String.valueOf(code));
    }
    /**
     * 短信验证
     */
    public boolean validatePhone(String phoneNumber,String code) throws Exception{
        //比对验证码，更新用户激活状态
        //1，比对验证码
        String key="activation:"+phoneNumber;
        String value=redisAPI.get(key);
        if (null!=value&&value.equals(code)) {
            ItripUser user = this.findUserByUserCode(phoneNumber);
            if (null!=user){
                //2，更新用户激活状态
                user.setActivated(1);
                user.setFlatID(user.getId());
                user.setUserType(0);
                itripUserMapper.updateItripUser(user);
                return true;
            }
        }
        return false;
    }

    @Override
    public ItripUser findUserByUserCode(String userCode) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("userCode",userCode);
        List<ItripUser> itripUsers=itripUserMapper.getItripUserListByMap(map);
        if (itripUsers.size()>0){
            return itripUsers.get(0);
        }else {
            return null;
        }
    }

    /**
     * 登录：用户名密码验证
     * @param userCode
     * @param userPassword
     * @return
     * @throws Exception
     */
    @Override
    public ItripUser login(String userCode, String userPassword) throws Exception {
        ItripUser user=this.findUserByUserCode(userCode);
        if (null!=user&&user.getUserPassword().equals(userPassword)){
            //判断用户状态是否激活
            if (user.getActivated()!=1){
                throw new Exception("用户未激活");
            }
            return user;
        }
        return null;
    }

}
